package Observer_pattern;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Bjarke
 * Date: 14-10-13
 * Time: 17:24
 * To change this template use File | Settings | File Templates.
 */
public class StockGrabber implements Subject {

    private ArrayList<Observer> observers;
    private double ibmPrice;
    private double aaplPrice;
    private double googPrice;

    public StockGrabber() {
        observers = new ArrayList<Observer>();
    }

    @Override
    public void register(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void unregister(Observer deleteObserver) {
        int observerIndex = observers.indexOf(deleteObserver);

        System.out.println("Observer index: " + (observerIndex + 1) + " deleted");

        observers.remove(observerIndex);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observers){

            observer.update(ibmPrice, aaplPrice, googPrice);
        }
    }

    public void setIBMPrice(double ibmPrice){

        this.ibmPrice = ibmPrice;
        notifyObserver();
    }

    public void setAAPLPrice(double aaplPrice){
        this.aaplPrice = aaplPrice;
        notifyObserver();
    }

    public void setGOOGPrice(double googPrice){
        this.googPrice = googPrice;
        notifyObserver();
    }
}
